package cn.singlewindow.ha.util.collector.integration.component;

import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.messaging.Message;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 生成文件名
 * 格式：前缀_yyyyMMddHHmmssSSS_名称.后缀
 * message获取的名字：名称.后缀
 */
public class AppFileNameGenerator implements FileNameGenerator {

    /**
     * 前缀，名字，后缀
     */
    private String prefix;
    private String name;
    private String suffix;

    private String dateTimeFormater = "yyyyMMddHHmmssSSS";
    private String spliter = "_";
    public AppFileNameGenerator(){}
    public AppFileNameGenerator(String prefix,String name,String suffix){
        this.prefix = StringUtils.hasLength(prefix)?prefix+spliter:"";
        this.name = StringUtils.hasLength(name)?name:"";
        this.suffix = StringUtils.hasLength(suffix)?suffix:"";
    }
    @Override
    public String generateFileName(Message<?> message) {
        String dateTimeString = new SimpleDateFormat(dateTimeFormater).format(new Date())+spliter;
        String filename = (String) message.getHeaders().get(FileHeaders.FILENAME);
        filename = StringUtils.hasLength(filename)?filename:message.getHeaders().getId() + ".msg";
//        filename = message.getPayload() instanceof File ? ((File)message.getPayload()).getName() : (StringUtils.hasLength(name)?name+suffix:message.getHeaders().getId() + ".msg");
        filename = prefix+dateTimeString +filename;
        return filename;
    }
}
